/**
 * 
 */
package ua.kiev.ntukpi.asu.ga.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import ua.kiev.ntukpi.asu.ga.Individual;
import ua.kiev.ntukpi.asu.ga.Job;
import ua.kiev.ntukpi.asu.ga.Utils;
import ua.kiev.ntukpi.asu.ga.interfaces.Mutation;

/**
 * @author Alexander Galibey
 *
 */
public class MutationPlain implements Mutation{

	private static final String NAME = "Plain Mutation";


	public void mutate(List<Individual> selection, double probability) {
		
		for (Individual individual : selection){
			
			mutate(individual, probability);
		}
	}

	public void mutate(Individual selection, double probability) {
		
		List<Job> jobs = selection.getJobs();
		List<Integer> indexes = new ArrayList<Integer>();
		List<Job> mutatedJobs = new ArrayList<Job>();
		for (Job job : jobs){
			
			if (Utils.playAction(probability)){
				
				indexes.add(jobs.indexOf(job));
				mutatedJobs.add(job);
			}
		}
		Collections.shuffle(mutatedJobs);
		for (int i = 0; i < mutatedJobs.size(); i++){
			
			jobs.set(indexes.get(i), mutatedJobs.get(i));
		}
	}

	
	public String getName() {
		
		return NAME;
	}


}
